package com.yxjme.egcviewlibrary.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;
import java.util.ArrayList;
import java.util.List;

public class ECGView2 extends View {

    /*当前页面的宽度*/
    private int width ;
    /*y轴中间*/
    private int y_center ;
    /*每两个点之间距离*/
    private int temp_x = 3 ;
    /*画笔*/
    private Paint paint=new Paint();
    /*绘制的所有点*/
    private Path path = new Path();
    /*移动*/
    private float changed_x ;
    /*绘制多个点*/
    int count ;

    public void setTemp_x(int count) {
        this . count = count ;
    }

    public ECGView2(Context context) {
        this(context,null);
    }

    public ECGView2(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ECGView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }


    /**
     * 设置颜色
     * @param color
     * @return
     */
    public ECGView2 setPathColor(int color){
        paint.setColor(color);
        return ECGView2.this ;
    }



    /**
     * 设置线条的宽度
     * @param w
     * @return
     */
    public ECGView2 setPathWidth(int w){
        paint.setStrokeWidth(w);
        return  ECGView2.this ;
    }


    private void initPaint() {
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setDither(true);
        paint.setStrokeWidth(10);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.WHITE);
        /*使每个点连接处更平顺*/
        CornerPathEffect cornerPathEffect = new CornerPathEffect(200);
        paint.setPathEffect(cornerPathEffect);
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(MeasureSpecWidth(widthMeasureSpec),MeasureSpecHeight(heightMeasureSpec));
    }

    /**
     * 测量view 高度
     * @param heightMeasureSpec
     * @return
     */
    private int MeasureSpecHeight(int heightMeasureSpec) {
        final int minimumHeight = getSuggestedMinimumHeight();
        int h = 0 ;
        int heightMode = View.MeasureSpec.getMode(heightMeasureSpec);
        int height = View.MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode == View.MeasureSpec.EXACTLY){
            /*固定值*/
            h =  height ;
        }else {
            if (heightMode == View.MeasureSpec.AT_MOST){
                h =  getPaddingBottom() + getPaddingTop();
            }else {
                h = Math.max(minimumHeight, height);
            }
        }
        return h;
    }

    /**
     * 测量view的宽度
     *
     * @param widthMeasureSpec
     * @return
     */
    private int MeasureSpecWidth(int widthMeasureSpec) {
        final int minimumWeight =getSuggestedMinimumWidth ();
        int widthMode = View.MeasureSpec.getMode(widthMeasureSpec);
        int width = View.MeasureSpec.getSize(widthMeasureSpec);
        int w = 0 ;
        if (widthMode == View.MeasureSpec.EXACTLY){
            /*固定值*/
            w =  width ;
        }else {
            if (widthMode == View.MeasureSpec.AT_MOST){
                w =  getPaddingBottom() + getPaddingTop();
            }else {
                w = Math.max(minimumWeight, width);
            }
        }
        return w ;
    }



    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if (width==0){
            width = getWidth() ;
            y_center = getHeight() / 2 ;

            /**/
            if (count!=0){
                temp_x = width / count ;
            }

            path.moveTo(width , y_center);
        }
        super.onLayout(changed, left, top, right, bottom);
    }



    Canvas canvas ;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
        this.canvas = canvas ;

        /*每次绘制都需要重置*/
        changed_x = 0 ;
        path.reset();
        path.moveTo(width,y_center);

        for (int i=0 ;i < list.size();i++){
            changed_x += temp_x ;
            if (i==list.size()-1){
                path.lineTo(width - changed_x ,y_center);
            }else {
                int y = y_center;
                if(i%2==0){
                    y= (int) getsY(list.get(i));
                }
                path.lineTo(width - changed_x,y);
            }
        }


        path.lineTo(0,y_center);
        /*绘制路径*/
        canvas.drawPath(path,paint);
    }



    /**
     * 更新绘制
     *
     * @param data
     */
    public void setData(String data){
        /*总移动距离*/
        list.add(data);
        invalidate();
    }


    /**
     * 所有点
     */
    private List<String> list = new ArrayList<>();


    /**
     * 当前Y值
     *
     * @return
     */
    public float getsY(String data){
        int y_int = Integer.parseInt(data);
        y_int = (y_int - 2048) *(-1);
        float   y_coor = y_int *1/2 + y_center;
        return  y_coor;
    }



    public void reset(){
        list.clear();
        changed_x = 0 ;
        path.reset();
        path.moveTo(width,y_center);
        invalidate();
    }



    /**
     * 清除画板
     */
    public void clear(){
        Paint paint = new Paint();
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        canvas.drawPaint(paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    }
}
